{template 'public/header'}
{template 'public/comhead'}
<ul class="nav nav-tabs">
    <li {if $operation == 'display'}class="active"{/if}><a href="{php echo $this->createWebUrl('type', array('op' => 'display'))}">门店类型管理
</a></li>
    <li {if $operation == 'post'}class="active"{/if}><a href="{php echo $this->createWebUrl('type', array('op' => 'post'))}">添加门店类型</a></li>
</ul>
{if $operation == 'post'}
<div class="main">
    <form action="" method="post" class="form-horizontal form" enctype="multipart/form-data">
        <input type="hidden" name="parentid" value="{$parent['id']}" />
        <div class="panel panel-default">
            <div class="panel-heading">
                门店类型编辑
            </div>
            <div class="panel-body">
                {if !empty($parentid)}
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">上级分类</label>
                    <div class="col-sm-9">
                        {$parent['name']}
                    </div>
                </div>
                {/if}
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">排序</label>
                    <div class="col-sm-9">
                        <input type="text" name="displayorder" class="form-control" value="{$type['displayorder']}" />
                    </div>
                </div>
                {if $setting['is_school'] == 1}
                {if $_W['role'] == 'founder' || $_W['role'] == 'manager'}
                <div class="form-group">
                    <label class="col-xs-12 col-sm-2 col-md-2 col-lg-2 control-label">所属分站</label>
                    <div class="col-sm-10 col-lg-9">
                        <select class="form-control" name="schoolid" id="schoolid">
                            <option value="0">请选择分站</option>
                            {loop $school $row}
                            <option value="{$row['id']}" {if $type['schoolid']==$row['id'] }selected{/if}>{$row['title']}</option>
                            {/loop}
                        </select>
                    </div>
                </div>
                {/if}
                {/if}
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">类型名称</label>
                    <div class="col-sm-9">
                        <input type="text" name="catename" class="form-control" value="{$type['name']}" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">类型图标</label>
                    <div class="col-sm-9">
                        {php echo tpl_form_field_image('thumb', $type['thumb'])}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">链接</label>
                    <div class="col-sm-9">
                        <input type="text" name="url" class="form-control" value="{$type['url']}" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span>位置导航</label>
                    <div class="col-sm-9 col-xs-12 form-inline">
                        <input type="text" name="lng" value="{$type['lng']}" placeholder="经度" class="form-control"/>
                        <input type="text" name="lat" value="{$type['lat']}" placeholder="纬度"  class="form-control"/>
                        <button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">选择坐标</button>
                        <span class="help-block" style="color: red;">如果使用，请先申请腾讯地图API，在系统设置中对应位置</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group col-sm-12">
            <input type="submit" name="submit" value="保存设置" class="btn btn-primary col-lg-3" />
            <input type="hidden" name="token" value="{$_W['token']}" />
        </div>
    </form>
</div>
<div class="modal fade bs-example-modal-lg" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>

                <h4 class="modal-title" id="myModalLabel">请选择地点</h4>

            </div>

            <div class="modal-body">

                <div class="input-group" style="margin-bottom:10px">

                    <input type="text" id="mapaddress" class="form-control"  placeholder="请输入地址来直接查找相关位置">

                    <span class="input-group-btn">

          <button class="btn btn-default" onclick="codeAddress()" type="button">&nbsp;&nbsp;搜索&nbsp;&nbsp;</button>

          </span> </div>

                <div style="width:100%;height:400px" id="container"></div>

            </div>

            <div class="modal-footer">

                <input type="hidden" id="map_lng" class="form-control" />

                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>

                <button type="button" class="btn btn-primary" onclick="chooseMap()">确认</button>

            </div>

        </div>

    </div>

</div>
{if !empty($setting['qqmapapi'])}
<script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key={$setting['qqmapapi']}"></script>
<script src="{RES}/mobile/style1/script/geolocation.js?Dj8" type="text/javascript"></script>
<script>
    var geocoder,map,marker = null;
    var position_arr = new Array();
    var mkey = "{$setting['qqmapapi']}";

    if (mkey == '') {
        alert('腾讯地图key没有设置');
        exit;
    }

    $(document).ready(function(e) {
        var lat= $("input[name='lat']").val();
        var lng= $("input[name='lng']").val();

        if (lat == '') {
            var geolocation = new qq.maps.Geolocation(mkey, "myapp");
            geolocation.getLocation(setPoint, function () {
            }, {timeout:4000, failTipFlag:true});
        } else {
            position_arr.lat = lat;
            position_arr.lng = lng;
            setPoint(position_arr);
        }
    });

    var setPoint = function(position) {
        var mapbegin_lat = (position.latitude || position.lat);
        var mapbegin_lng = (position.longitude || position.lng);

        $("input[name='lat']").val(mapbegin_lat);
        $("input[name='lng']").val(mapbegin_lng);

        var center = new qq.maps.LatLng(mapbegin_lat,mapbegin_lng);
        map = new qq.maps.Map(document.getElementById('container'),{
            center: center,
            zoom: 15
        });

        var marker = new qq.maps.Marker({
            position: center,
            draggable: true,
            map: map
        });

        qq.maps.event.addListener(map, 'click', function(event) {
            marker.setMap(null);
            marker=new qq.maps.Marker({
                position:event.latLng,
                draggable: true,
                map:map
            });

            $("#map_lng").val(event.latLng);
        });

        qq.maps.event.addListener(marker, 'mouseup', function(event) {

            marker.setMap(null);

            marker=new qq.maps.Marker({

                position:event.latLng,

                draggable: true,

                map:map

            });

            $("#map_lng").val(event.latLng);

        });

        geocoder = new qq.maps.Geocoder({
            complete : function(result){
                map.setCenter(result.detail.location);
                marker.setMap(null);
                marker = new qq.maps.Marker({
                    map:map,
                    draggable: true,
                    position: result.detail.location
                });
                $("#map_lng").val(result.detail.location);
            }
        });
    }

    function codeAddress() {
        var address = document.getElementById("mapaddress").value;
        geocoder.getLocation(address);
    }

    function chooseMap(){
        if($("#map_lng").val().indexOf(",")>0){
            mapary=$("#map_lng").val().split(",");
            $("input[name='lat']").val(mapary[0]);
            $("input[name='lng']").val(mapary[1]);
        }
        $('#myModal').modal('hide');
    }
</script>
{/if}
{elseif $operation == 'display'}
<div class="main">
    <div class="panel panel-default">
        <form action="" method="post" class="form-horizontal form" >
            <input type="hidden" name="storeid" value="{$storeid}" />
            <div class="table-responsive panel-body">
                <table class="table table-hover">
                    <thead class="navbar-inner">
                    <tr>
                        <th style="width:100px;">显示顺序</th>
                        <th style="width:15%;">名称</th>
                        <th style="width:10%;">图标</th>

                        <th style="width:50%;">网址</th>
                        <th style="text-align:right;">操作</th>
                    </tr>
                    </thead>
                    <tbody id="level-list">
                    {loop $list $row}
                    <tr>
                        <td><input type="text" class="form-control" name="displayorder[{$row['id']}]" value="{$row['displayorder']}"></td>
                        <td><div class="type-parent">
                            {if $row['schoolid'] > 0}
                            {loop $school $item}
                            {if $row['schoolid'] == $item['id']}
                            [
                            {$item['title']}
                            ]
                            {/if}
                            {/loop}
                            {/if}


                            {$row['name']}&nbsp;&nbsp;</div>
                        </td>
                        <td>
                            <img src="{php echo tomedia($row['thumb']);}" onerror="this.src='../addons/weisrc_dish/template/images/shop_logo.png';" width="60px;" style="border-radius: 3px;">
                        </td>

                        <td>
                            {if empty($row['url'])}
                            <input type="text" class="form-control" name="url[{$row['id']}]" value="{$_W['siteroot']}app/index.php?i={$_W['uniacid']}&c=entry&typeid={$row['id']}&sid={$row['schoolid']}&do=waprestlist&m=weisrc_dish">
                            {else}
                            <input type="text" class="form-control" name="url[{$row['id']}]" value="{$row['url']}">
                            {/if}
                        </td>
                        <td style="text-align:right;"><a class="btn btn-default btn-sm" href="{php echo $this->createWebUrl('type', array('op' => 'post', 'id' => $row['id']))}" title="编辑">改</a>&nbsp;&nbsp;<a class="btn btn-default btn-sm" href="{php echo $this->createWebUrl('type', array('op' => 'delete', 'id' => $row['id']))}" onclick="return confirm('确认删除此分类吗？');return false;" title="删除">删</a></td>
                    </tr>
                    {/loop}
                    <tr>
                        <td colspan="5">
                            <input name="submit" type="submit" class="btn btn-primary" value="批量排序">
                            <input type="hidden" name="token" value="{$_W['token']}" />
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </form>
    </div>
    {$pager}
</div>
{/if}
{if $_W['isfounder']}
{template 'common/footer'}
{else}
{template 'public/footer'}
{/if}